package com.hui.servlet.student;

import com.hui.service.StudentService;
import com.hui.util.ServiceFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 李昭辉
 * @Date: 2021/06/20/13:52
 * @Description:
 */
@WebServlet(name = "DeleteServlet", urlPatterns = "/delete")
public class DeleteServlet extends HttpServlet {
    private StudentService studentService = ServiceFactory.getStudentService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String classId = req.getParameter("classId");
        boolean b = studentService.deleteStudentById(Integer.parseInt(id), Integer.parseInt(classId));

        PrintWriter writer = resp.getWriter();
        if (b) {
            writer.write("<script>window.alert('删除成功!');window.location.href='pageStudent'</script>");
        } else {
            writer.write("<script>window.alert('删除失败!!!');window.location.href='pageStudent'</script>");
        }
        writer.close();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
